package com.qf.serviceimpl;

import com.qf.constant.StrategyConstants;
import com.qf.dfa.SensitiveWordFilter;
import com.qf.model.StandardSubmit;
import com.qf.service.FilterChainService;
import com.qf.util.PushMsgUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 敏感词过滤策略
 *
 * @author Ypl
 * @version V1.0
 * @Project OctoberAndNovember
 * @Package com.qf.serviceimpl
 * @Description:
 * @Date 2022/11/10 10:49
 */
@Service(value = "dirtyWords")
@Slf4j

public class DirtyWordFilterServiceImpl implements FilterChainService {

    @Autowired
    PushMsgUtil pushMsgUtil;

    @Override
    public boolean strategy(StandardSubmit standardSubmit) {
        log.debug("【敏感词策略】 开始执行");
        //读取敏感库中的数据
        SensitiveWordFilter instance = SensitiveWordFilter.getInstance();
        // 检查文字中的敏感词
        int i = instance.checkSensitiveWord(standardSubmit.getMessageContent(), 0, 2);
        //如果i>0  说明有敏感词
        if(i>0){
            // 发送失败 将日志存入MQ
            log.debug("【扣费策略】 因有敏感词发送到日志中");
            pushMsgUtil.sendLog(standardSubmit, StrategyConstants.STRATEGY_ERROR_DIRTY_WORDS);
            pushMsgUtil.pushReport(standardSubmit,StrategyConstants.STRATEGY_ERROR_DIRTY_WORDS);
            return false;
        }
        log.debug("【敏感词策略】 执行结束");
        return true;
    }
}
